<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>瑞成火锅</title>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <script src="https://unpkg.com/vue@2/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
  <link href="https://cdn.staticfile.net/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.staticfile.net/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<style>
  * {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }

  a {
    text-decoration: none;
    color: #fff;
    font-size: 18px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #fff;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<body>
  <div id="home">
    <div style="display: flex;">
      <el-row class="tac">
        <el-col :span="6">
          <el-menu class="el-menu-vertical-demo" background-color="#471306" text-color="#fff"
            active-text-color="#ffd04b" style="height: 100vh; width: 300px; padding: 55% 15%;">
            <el-menu-item>
              <img src="../img/logo3.png" width="150px" alt="">
            </el-menu-item>
            <el-menu-item>
              <span slot="title"><a href="./home.html">首页</a></span>
            </el-menu-item>
            <el-menu-item>
              <span slot="title"><a href="./addgoods.html">添加食材</a></span>
            </el-menu-item>
            <el-menu-item>
              <span slot="title"><a href="./orders.html">订单管理</a></span>
            </el-menu-item>
            <el-menu-item>
              <span slot="title"><a href="./user.html">用户管理</a></span>
            </el-menu-item>
            <el-menu-item>
              <span slot="title"><a href="./message.html">留言管理</a></span>
            </el-menu-item>
            <el-menu-item>
              <span slot="title"><a href="./contact.html">联系管理</a></span>
            </el-menu-item>
            <el-menu-item>
              <span slot="title"><a href="./userinfo.html">个人信息</a></span>
            </el-menu-item>
          </el-menu>
        </el-col>
      </el-row>
      <div style="width: 100%; background: url(../img/bg.jpg) no-repeat; background-size: 100% 100%;">
        <div
          style="background-color: #471306;height: 80px; width: 100%;overflow: hidden;padding-top: 1.3%; padding-left: 5%;">
          <div style="display: flex;">
            <h2 style="color: #fff; margin-right: 3%;">个人信息</h2>
            <div style="margin-left: auto;margin-right: 10%;padding-top: 0.5%;">
              <span slot="title"><a href="./login.html">退出登录</a></span>
            </div>
          </div>
        </div>
        <div style="padding: 5% 10%;">
          <!-- 主体内容 -->
          <div
            style="background-color: rgba(255, 255, 255, .9);padding: 10px;min-height: 600px; width: 700px; margin: auto;">
            <h2>个人信息</h2>
            <el-form style="padding-top: 1%;" :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm">
              <el-form-item label="用户名" prop="name">
                <el-input type="text" v-model="ruleForm.name" autocomplete="off" style="width: 60%;"></el-input>
              </el-form-item>
              <el-form-item label="头像" prop="img">
                <el-upload class="avatar-uploader" :action="url+'/upload'" :show-file-list="false"
                  :on-success="handleAvatarSuccess">
                  <img v-if="imageUrl" :src="url+'/download/'+imageUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="账号" prop="acccount">
                <el-input type="text" v-model="ruleForm.acccount" disabled style="width: 70%;"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input type="text" v-model="ruleForm.password" disabled style="width: 70%;"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input type="text" v-model="ruleForm.email" style="width: 70%;"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="updatePass()">修改密码</el-button>
                <el-button type="primary" @click="submitForm()">确认</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
    <!-- 修改密码 -->
    <el-dialog title="修改个人密码" :visible.sync="dialogVisible" width="40%">
      <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="acccount">
          <el-input type="password" v-model="ruleForm.acccount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updatePassExit()">取 消</el-button>
        <el-button type="primary" @click="updatePassSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</body>
<script>
  new Vue({
    el: '#home',
    data() {
      return {
        ruleForm: {},
        dialogVisible: false,
        imageUrl: '',
        url: 'http://localhost:9092/common',
      }
    },
    created() {
      if(localStorage.getItem("userId") == null){
        window.location.href = './login.html';
        return;
      }
      this.page_init();
    },
    methods: {
      // 取消修改密码
      updatePassExit(){
        this.dialogVisible = false;
        this.page_init();
      },
      // 图片上传成功时调用
      handleAvatarSuccess(res, file) {
        this.imageUrl = res.data;
      },
      // 确定修改密码
      async updatePassSubmit() {
        if (this.ruleForm.pass !== this.ruleForm.password) {
          this.$message.error("请保持两次输入的密码一致!");
          return;
        }
        // this.ruleForm.pass = "";
        this.ruleForm.id = localStorage.getItem("userId");
        const { data: result } = await this.$http.post('http://localhost:9092/hot_userback/updatePass', this.ruleForm);
        if (result.flag) {
          this.$message.success(result.message)
          window.location.href = './login.html';
        } else {
          this.$message.error(result.message)
        }
        this.dialogVisible = false;
      },
      // 修改密码
      updatePass() {
        this.dialogVisible = true;
        this.ruleForm = {};
      },
      // 确认修改用户信息
      async submitForm() {
        this.ruleForm.id = localStorage.getItem("userId");
        this.ruleForm.img = this.imageUrl;
        const { data: result } = await this.$http.post('http://localhost:9092/hot_userback/update', this.ruleForm);
        if (result.flag) {
          this.$message.success(result.message)
        } else {
          this.$message.error(result.message)
        }
        this.page_init();
      },
      // 刷新页面
      async page_init() {
        const { data: result } = await this.$http.get('http://localhost:9092/hot_userback/queryOne/' + localStorage.getItem('userId'));
        // 赋值数据
        this.ruleForm = result.l;
        // 赋值图片
        this.imageUrl = this.ruleForm.img;
      }
    }
  })
</script>

</html>